#####
# Replication for: "Can Political Speech Foster Tolerance of Immigrants?" by Schleiter, Tavits, and Ward.
# Table S.3
#####

library(here)
library(data.table)
library(xtable)

# load the pooled data if not already in workspace
if(!exists("pooled")){
  pooled <- fread(here("data", "pooled.csv"))
}


# Create variable name objects --------------------------------------------

outcomes_main <- c(
  "Comprehension Item 1" = "attention_1",
  "Comprehension Item 2" = "attention_2",
  "Comprehension Item 3" = "attention_3",
  "Norms Statement" = "mechanism_NM",
  "Countering Stereotypes Statement" = "mechanism_CS",
  "Common Humanity Statement" = "mechanism_CH",
  "Immigrant Neighbors" = "imm_neighbors2",
  "Increase Immigration" = "imm_increase2",
  "Immigration Index" = "immPCA",
  "Second Order Beliefs" = "second_order_beliefs2",
  "Discomfort Criticizing Immigrants" = "prejudice_discomfort2"
)

treatments_main <- c(
  "Treatment: Norms" = "NM",
  "Treatment: Countering Stereotypes" = "CS",
  "Treatment: Common Humanity" = "CH",
  "Treatment: Sleep (control)" = "Treatment == \"Sleep\""
)

covariates_main <- c(
  "Age: 18-29" = "age %in% 18:29",
  "Age: 30-39" = "age %in% 30:39",
  "Age: 40-49" = "age %in% 40:49",
  "Age: 50-59" = "age %in% 50:59",
  "Age: 60+" = "age >= 60",
  "Female" = "female",
  "Race: White/Caucasian" = "race2 == \"White/Caucasian\"",
  "Race: Asian" = "race2 == \"Asian\"",
  "Race: African American/Black" = "race2 == \"Black/African American\"",
  "Race: Hispanic/Latino" = "race2 == \"Hispanic/Latino\"",
  "Race: Other" = "race2 == \"Other (please specify)\" | is.na(race2)",
  "Education: Less than high school" = "edu2 == \"Less than high school\"",
  "Education: High school graduate/GED" = "edu2 == \"High school graduate/ GED\"",
  "Education: 2 year degree" = "edu2 == \"2 year degree\"",
  "Education: Some college" = "edu2 == \"Some college\"",
  "Education: 4 year degree" = "edu2 == \"4 year degree\"",
  "Education: Masters degree" = "edu2 == \"Masters degree\"",
  "Education: Professional Degree (JD, MD)" = "edu2 == \"Professional Degree (JD, MD)\"",
  "Education: Doctorate" = "edu2 == \"Doctorate\"",
  "Party ID: Democrat" = "pid2 == \"Democrat\"",
  "Party ID: Republican" = "pid2 == \"Republican\"",
  "Party ID: Independent" = "pid2 == \"Independent\"",
  "Party ID: Something else" = "pid2 == \"Something else\"",
  "News Attention" = "news2",
  "Region: Northeast" = "region2 == \"Northeast\"",
  "Region: Midwest" = "region2 == \"Midwest\"",
  "Region: South" = "region2 == \"South\"",
  "Region: West" = "region2 == \"West\""
)

covariates_wave <- c(
  "Survey: Main, Wave 1" = "wave == \"1\"",
  "Survey: Main, Wave 2" = "wave == \"2\"",
  "Survey: Main, Wave 3" = "wave == \"3\"",
  "Survey: Replication" = "wave == \"Replication\""
)

# Create table ------------------------------------------------------------

descriptives_rep <-
  data.frame(
    t(
      pooled[ , lapply(
        c(outcomes_main, treatments_main, covariates_main, covariates_wave),
        function(x){
          c("min" = min(eval(parse(text = x)), na.rm = T),
            "mean" = mean(eval(parse(text = x)), na.rm = T),
            "max" = max(eval(parse(text = x)), na.rm = T),
            "sd" = sd(eval(parse(text = x)), na.rm = T),
            "N" = length(na.omit(eval(parse(text = x)))))
        }
      )]
    )
  )

colnames(descriptives_rep) <- c("Min","Mean", "Max", "Std. Dev.", "N")

descriptives_rep$N <- prettyNum(descriptives_rep$N, big.mark = ",")

xtable(
  rbind(descriptives_rep),
  digits = c(0,0,3,0,3,0),
  label = "tab:descriptives",
  caption = "Descriptive Statistics (Pooling Main and Replication Studies)",
  align = c("l", "c","c","c","c","c")
)
